<%- include("components/header",{type:'create'}) %>

<div id="fh5co-page">
	<div id="fh5co-main">
		<div class="posts">
            <form class="form-horizontal" method="post">
                <div class="form-group">
                    <label for="inputTitle" class="col-sm-2 control-label">标题</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="title" id="title" placeholder="不能超过30个字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputContent" class="col-sm-2 control-label">内容</label>
                    <div class="col-sm-10">
                        <textarea id="summernote" name="content"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <a href="javascript:;" id="submit" class="btn btn-default">提交</a>
                        <!-- <button type="submit" id="submit" class="btn btn-default">提交</button> -->
                    </div>
                </div>
            </form>
        </div>
	</div>
</div>

<script>
    $(function(){
        $('#summernote').summernote({
            tabsize: 2,
            height: 300
        });
        $('#submit').click(()=>{
            if ($('#title').val().trim() == '') {
                fade('请输入标题')
            }else if ($('#summernote').val().trim() == '') {
                fade('请输入内容')
            }else{
                $.ajax({
                    url: "/create",
                    data: $('form').serialize(),
                    type: "POST",
                    cache: false,
                    dataType: 'json',
                    success: function (msg) {
                        if (msg) {
                            fade('发表成功')
                            setTimeout(()=>{
                                window.location.href = "/posts"
                            },1000)
                        }else{
                            fade('发表失败')
                        }
                    },
                    error: function () {
                        console.log('异常');
                    }
                })			
            }   
        });
    })
</script>

<%- include("components/footer") %>